/**
 * Created by yangh on 2016/09/04.
 */
var main = {};
var myScroll;
(function ($) {
    main.init = function () {
        main.initEvent();
    };

    main.initEvent = function() {
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
        if (myScroll.y < main.max - 50) {
        	
        	//加载数据
        	main.loadData(0);
            
            this.refresh();
			main.max = this.maxScrollY;
			console.log(main.max);
        }
    }
    
    
    /** 
     * function loadData
     */
    main.loadData = function(isReplace){
    	if(isReplace==1){
    		$("#storeList").html("");
    	}
    	//ajax begin
    	 var name=$("#name").val();
    	 var index = 0;
    	 if($("#storeList li").length!=0){
    		 index=$("#storeList li:last").data("id");
    	 }
         var html = "";
         $.ajax({
     		type:"post",
     		async:true, 
        	url:path+"/vouchermarket/merchant/search/more",
        	data:{
        		name:name,
        		index:index
        	},
     		success:function(data){	
     			if('000' == data.code){
     				var html = "";
     				for (var i = 0; i < data.result.length; i++) {
     					html +="<li data-id='"+data.result[i].id+"'>";
     					var url = path+"/vouchermarket/merchant/"+data.result[i].id;
 						html +="<a href='"+url+"'>";
 						html +="<div class=\"fImg\">";
 						html +="<img src='"+data.result[i].picurl+"'>";
 						html +="</div>";
 						html +="<h3>"+data.result[i].name+"</h3>";
 						html +="<p>";
 						html +="<span class=\"scoreCon\" data-score="+data.result[i].avgScore+"></span>";
 						html += "</p>";
 						html +="<p>"+data.result[i].goodsName+"</p>";
 						html +="</a>";
 						html +="</li>";
     				}	
     				
     				if(isReplace==1){
     					$("#storeList").html(html);
     				}else{
     					$("#storeList").append(html);
     				}
     				
     				if(data.result.length==0){
    					$("#tip").text("暂无结果");
    				}else if(data.result.length<20){
        				$("#loading").hide();
		  	            $("#tip").hide();
    				}else if(data.result.length>=20){
  		            	$("#loading").hide();
		  	            $("#tip").show();
		  	            canLoad=true;
	  		        }
     				
     				 /* 评分 */
     		        score($(".scoreCon"));
     				
     			}
     		}
     	})
   		//ajax end
    }

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll(".storeList", {
            probeType: 3,
            mouseWheel: true,
            click: true,
            preventDefault: false
        });

        main.max = myScroll.maxScrollY;
        console.log(main.max);

        myScroll.on("scroll", move);
    };
})(jQuery);

main.init();
//加载数据
main.loadData(1);

window.onload = function () {
    main.scroll();
};